import os
import pickle

import yaml
from sklearn.metrics import accuracy_score

from .feature_engineering import pre_training


def load_config(filepath):
    with open(filepath, 'r') as file:
        config = yaml.safe_load(file)
    return config


def predict_ssc_model(modelname):
    project_path = os.environ.get("PROJECT_PATH")
    config_path = os.path.join(project_path, 'config', 'ssc_config.yaml')
    config = load_config(config_path)

    _, x_test_scaled, _, y_test = pre_training()

    with open(config["models"][modelname]["model_path"], 'rb') as file:
        rf_model_loaded = pickle.load(file)

    # 预测
    y_pred_rf = rf_model_loaded.predict(x_test_scaled)

    # 评估模型
    rf_accuracy = accuracy_score(y_test, y_pred_rf)
    if modelname == 'random_forest':
        print(f"随机森林准确率: {rf_accuracy:.2f}")
    elif modelname == 'logisticRegression':
        print(f"逻辑回归准确率: {rf_accuracy:.2f}")
    elif modelname == 'knn':
        print(f"knn准确率: {rf_accuracy:.2f}")

    return y_pred_rf, rf_accuracy
